import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),

      home: VideoPlayerScreen(),
    );
  }
}

class VideoPlayerScreen extends StatefulWidget {
  const VideoPlayerScreen({super.key});

  @override
  State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  VideoPlayerController? _videoPlayerController;
  @override
  void initState() {
    super.initState();

    _videoPlayerController = VideoPlayerController.asset(
      'asset/videos/test.mp4',
    );

    //监听
    _videoPlayerController!.addListener(() {
      if (_videoPlayerController!.value.isPlaying) {
        setState(() {});
      }
    });

    _videoPlayerController!.initialize().then((_) {
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    super.dispose();

    if (_videoPlayerController != null) {
      _videoPlayerController!.dispose();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('视频播放器')),
      body: SizedBox.expand(
        child: Column(
          children: [
            //视频播放器
            buildAspectRatio(),

            SizedBox(height: 40),

            Row(
              children: [
                SizedBox(width: 10),
                //当前播放的进度
                Text(fformatDuration(_videoPlayerController!.value.position)),

                SizedBox(width: 10),
                //进度条
                Expanded(
                  child: VideoProgressIndicator(
                    _videoPlayerController!,
                    //内边距
                    padding: EdgeInsets.zero,
                    //拖动
                    allowScrubbing: true,
                    colors: VideoProgressColors(
                      playedColor: Colors.blue,
                      bufferedColor: Colors.grey,
                      backgroundColor: Colors.grey.shade300,
                    ),
                  ),
                ),

                SizedBox(width: 10),
                Text(fformatDuration(_videoPlayerController!.value.duration)),
                SizedBox(width: 10),
              ],
            ),
            SizedBox(height: 40),
            //操作按钮
            Wrap(
              spacing: 10,
              children: [
                ElevatedButton(
                  onPressed: () {
                    _videoPlayerController!.play();
                  },
                  child: Text('播放'),
                ),

                ElevatedButton(
                  onPressed: () {
                    _videoPlayerController!.pause();
                  },
                  child: Text('暂停'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget buildAspectRatio() {
    if (_videoPlayerController == null) {
      return Container();
    }

    if (!_videoPlayerController!.value.isInitialized) {
      return CircularProgressIndicator();
    }

    return AspectRatio(
      aspectRatio: _videoPlayerController!.value.aspectRatio,
      child: VideoPlayer(_videoPlayerController!),
    );
  }

  String fformatDuration(Duration duration) {
    final huours = duration.inHours;
    final minutes = duration.inMinutes.remainder(60);
    final seconds = duration.inSeconds.remainder(60);

    return '${huours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  }
}
